package kr.hkit.coffeemgn.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import kr.hkit.coffeemgn.DTO.CoffeeDTO;
import kr.hkit.framework.dao.CloseDaoUtil;

public class CoffeeDAO {
	private Connection con;
	private PreparedStatement pstmt;
	private ResultSet rs;

	public CoffeeDAO() {
	}

	public CoffeeDAO(Connection con) {
		this.con = con;
	}

	public void setCon(Connection con) {
		this.con = con;
	}

	public void insertProduct(CoffeeDTO dto) throws SQLException {
		String sql = "insert into saleorder values(?,?,?,?,?)";
		pstmt = con.prepareStatement(sql);
		pstmt.setString(1, dto.getpCode());
		pstmt.setString(2, dto.getpName());
		pstmt.setInt(3, dto.getpCost());
		pstmt.setInt(4, dto.getpCnt());
		pstmt.setInt(5, dto.getpRate());

		// System.out.println(pstmt);
		int row = pstmt.executeUpdate();
		CloseDaoUtil.close(pstmt);
	}

	public CoffeeDTO selectTotalList() throws SQLException{
		CoffeeDTO dto = null;
		String sql = "select totalsalecost, totaladdtax, totalpushcost, totalratecost from total_list";	
		pstmt = con.prepareStatement(sql);
		rs = pstmt.executeQuery();
		if (rs.next()){
			dto = new CoffeeDTO();
			dto.setTotalSale(rs.getInt(1));
			dto.setTotalTax(rs.getInt(2));
			dto.setTotalPush(rs.getInt(3));
			dto.setTotalRate(rs.getInt(4));
		}
		return dto;
	}
	
	public ArrayList<CoffeeDTO> selectAllCoffeeListOrderByRateCost() throws SQLException{
		ArrayList<CoffeeDTO> lists = new ArrayList<>();
		String sql = "select rank, code, name, cost, cnt, rate from list_orderby_rate";
		pstmt = con.prepareStatement(sql);
		rs = pstmt.executeQuery();

		while (rs.next()) {
			CoffeeDTO dto = new CoffeeDTO();
			dto.setRank(rs.getInt("rank"));
			dto.setpCode(rs.getString("code"));
			dto.setpName(rs.getString(3));
			dto.setpCost(rs.getInt(4));
			dto.setpCnt(rs.getInt(5));
			dto.setpRate(rs.getInt(6));
			lists.add(dto);
		}
		lists.add(selectTotalList());
		return lists;
	}
	public ArrayList<CoffeeDTO> selectAllCoffeeListOrderBySaleCost() throws SQLException {
		ArrayList<CoffeeDTO> lists = new ArrayList<>();
		String sql = "select (select count(*)+1 from saleorder where cost*cnt > s.cost*s.cnt) as rank ,	code, name, cost, cnt, rate from saleorder s order by cost*cnt desc";
		pstmt = con.prepareStatement(sql);
		rs = pstmt.executeQuery();

		while (rs.next()) {
			CoffeeDTO dto = new CoffeeDTO();
			dto.setRank(rs.getInt("rank"));
			dto.setpCode(rs.getString("code"));
			dto.setpName(rs.getString(3));
			dto.setpCost(rs.getInt(4));
			dto.setpCnt(rs.getInt(5));
			dto.setpRate(rs.getInt(6));
			lists.add(dto);
		}
		lists.add(selectTotalList());
		return lists;
	}

	public String selectNameByCode(String code) throws Exception {
		String sql = "select name from product_info where code = ?";
		pstmt = null;
		rs = null;
		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, code);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				return rs.getString("name");
			} else {
				throw new Exception("해당 제품이 존재하지 않음");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			CloseDaoUtil.close(pstmt);
		}
		return null;
	}
}
